<?php

class IndexAction extends Action{
	//个人空间首页
	public function index(){
		//获取博客列表
		$order = "digCount DESC";
		$field = 'title,blogId,commentCount,readCount,cTime,cover,category,content,uid,digCount';
		$map['status']	=	array('in',array(1,2));
		$time = time();
		$map['cTime']	=	array('between',array($time-259200,$time));
		$map['isHot']	=	0;
		$index['blog']	=	D('BlogInfo','blog')->getBlogList($map,$field,$order,7,true,200);
		
		//获取最新的博客
		$map2['status'] = array('in',array(1,2));
		$index['newBlog']  = D('BlogInfo','blog')->where($map2)->order('cTime DESC')->limit('0,10')->findAll();
		
		
		$index['feeds'] = x('Feed')->get('display=0',10);
		//$index['top']   =D('')->query("SELECT `aimId` as uid,count(`aimId`) as count FROM ".C("DB_PREFIX")."follow WHERE type='user' GROUP BY `aimId` HAVING count(`aimId`) >1 ORDER BY count DESC LIMIT 10");
		$index['top']   = D('')->query("SELECT `uid`,`userOrder` FROM ".C("DB_PREFIX")."blog_mention ORDER BY userOrder DESC limit 0,10");
		foreach ($index['top'] as $key=>$value){
			$info = M('space_search')->where("uid=".$value['uid']." AND info='department'")->field('value')->find();
			
			$index['top'][$key]['department'] = msubstr($info['value'],0,12);
		}
		
			//专家
		//$expert = model('UserGroup')->getGroupUsers();
		
		$expert = D('Space','home')->where('isexpert=1')->field('uid')->findall();
		$expert = getSubByKey( $expert ,'uid' );		
		
		if(!empty($expert)){
			$expert = array_slice($expert,0,20);
		}
		
		$index['expert']   =		$expert;
		$index['record']   = D('Record','home')->order('id DESC')->field('uid,content')->limit(10)->findall();
		$this->assign($index);
		$sopt['title'] = '首页 - 空间';
		$this->setTitle($sopt);				
		$this->display();
	}
	
	//计划任务，更新用户资料
	public function updateUserBaseInfo(){
	   set_time_limit(0);
	   header("Content-Type:text/html; charset=UTF-8");
	   $pBaseInfo = D('BaseInfo','home');
	   $pSpace = D('BaseInfo','home');
	   
	  $userInfo = D('User')->where('uid='.$_REQUEST['uid'].' AND active=1')->find();
   	  if( $hrbiInfo = $pBaseInfo->get_Hrbi_info($userInfo['uname']) ){	    
			$list = $pBaseInfo ->get_Hrbi_Info_Have_Data( $hrbiInfo );
			foreach($list as $key=>$value){
				$data[$key] = $value['value'];
			}
			$pSpace->save_Base_Data($userInfo['uid'],'baseinfo',$data);
			echo $userInfo['uid'] ." => ".$userInfo['uname'].'空间资料更新成功<br>';
			unset($hrbiInfo);
			unset($list);
	   }else{
	   		echo $userInfo['uid'] ." => ".$userInfo['uname'].'无Hrbi信息<br>';
	   }
	   
	   unset($pSpace);
	   unset($pBaseInfo);
	   unset($userInfo);


	   
	   if( $_REQUEST['type']=='auto' ){
			$nextId = D('User')->where('uid>'.$_REQUEST['uid'].' AND active=1')->field('uid')->order('uid ASC')->limit(1)->find();
		    if( $nextId ){
		      echo '<script>location.href="'.U('space/Index/updateUserBaseInfo',array('uid'=>$nextId['uid'],'type'=>'auto')).'"</script>';
		    }else{
		      echo '更新完成';
		    }
	    }
	}
	
	public function updateSpaceSearch(){
	   set_time_limit(0);
	   header("Content-Type:text/html; charset=UTF-8");
	   $pBaseInfo = D('BaseInfo','home');
	   $pSpace = D('BaseInfo','home');
	   
	  $userInfo = D('User')->where('uid='.$_REQUEST['uid'])->find();
//   	  if( $hrbiInfo = $pBaseInfo->get_Hrbi_info($userInfo['uname']) ){	    
//			$list = $pBaseInfo ->get_Hrbi_Info_Have_Data( $hrbiInfo );
//			foreach($list as $key=>$value){
//				$data[$key] = $value['value'];
//			}
//			$pSpace->save_Base_Data($userInfo['uid'],'baseinfo',$data);
//			echo $userInfo['uid'] ." => ".$userInfo['uname'].'空间资料更新成功<br>';
//			unset($hrbiInfo);
//			unset($list);
//	   }else{
//	   		echo $userInfo['uid'] ." => ".$userInfo['uname'].'无Hrbi信息<br>';
//	   }
	   $info = $pBaseInfo->get_Base_Data( $userInfo['uid'] );
	   foreach($info as $key=>$value){
	   		if($value['value']) $ddd[$value['field']] = $value['value'];
	   }
	   
	   $pSpace->save_Search_Data( $userInfo['uid'] , 'baseinfo' , $ddd );
	   
	   echo $userInfo['uid'];
	   
	   unset($info);
	   unset($ddd);
	   unset($userInfo);


	   
	   if( $_REQUEST['type']=='auto' ){
			$nextId = D('User')->where('uid>'.$_REQUEST['uid'])->field('uid')->order('uid ASC')->limit(1)->find();
		    if( $nextId ){
		      echo '<script>location.href="'.U('space/Index/updateSpaceSearch',array('uid'=>$nextId['uid'],'type'=>'auto')).'"</script>';
		    }else{
		      echo '更新完成';
		    }
	    }
	}	
}
?>